package com.ithiema.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ithiema.reggie.common.BaseContext;
import com.ithiema.reggie.common.CustomException;
import com.ithiema.reggie.common.GlobalConstant;
import com.ithiema.reggie.common.R;
import com.ithiema.reggie.entity.ShoppingCart;
import com.ithiema.reggie.service.ShoppingCartService;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 购物车控制器
 *
 * @author wangxiao
 * @since 2022/11/7 16:16
 */
@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;
    /**
     * 添加购物车
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCartParam){
        log.info("前后端联通");
//        1.查询当前此购物车中有没有此菜品或者套餐得数据
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
//                条件1.当前用户登录id,因为对于c端来说，用户只能看到自己得信息
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentUserId());
        /*
        判断当前购物车是否存在此菜品或者套餐
         */
        if (shoppingCartParam.getDishId() != null){
            queryWrapper.eq(ShoppingCart::getDishId,shoppingCartParam.getDishId());
        }else if (shoppingCartParam.getSetmealId() != null){
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCartParam.getSetmealId());
        }else {
            throw new CustomException("传递参数不正确");
        }
        ShoppingCart shoppingCart = this.shoppingCartService.getOne(queryWrapper);
        if (shoppingCart != null){
//            如果存在，就在原来的基础上加一
            shoppingCart.setNumber(shoppingCart.getNumber()+1);
//            更新资源
            this.shoppingCartService.updateById(shoppingCart);
        }else {
//            如果不存在则添加到购物车，默认数量就是一
//                    不存在对购物车初始化
            shoppingCart = new ShoppingCart();
//            不存在得商品添加到购物车
//            拿到前端得传入得数据 在赋值给 shoppingCart
            BeanUtils.copyProperties(shoppingCartParam,shoppingCart);
//            在 ThreadLocal中取到当前用户得id ，赋值给要添加购物车数据，及当前时间。
//            当前用户
            shoppingCart.setUserId(BaseContext.getCurrentUserId());
//            购物车数量
            shoppingCart.setNumber(1);
//            创建时间
            shoppingCart.setCreateTime(LocalDateTime.now());
//            新增数据
            this.shoppingCartService.save(shoppingCart);
        }
        return R.success(shoppingCart);
    }
    /**
     * 查询购物车列表
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> getList(){
        //当前用户
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
//    当前用户得购物车
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentUserId());
//                根据购物车创建时间倒叙
        queryWrapper.orderByDesc(ShoppingCart::getCreateTime);
//        调用业务层查询购物车 用list集合查询
        List<ShoppingCart> list = this.shoppingCartService.list(queryWrapper);
        return R.success(list);
    }
    /**
     * 清空购物车
     */
    @DeleteMapping("/clean")
    public R<String> clean(){
//        根据用户id，清空购物车
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentUserId());
//        调用业务层remove
        shoppingCartService.remove(queryWrapper);
        return R.success(GlobalConstant.SUCCESS);
    }
    /**
     *购物车减号
     */
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCartParam){
        log.info("前后端联通");
//        判断前端传入得是不是空
        if (shoppingCartParam.getSetmealId() == null && shoppingCartParam.getDishId() == null){
            throw new CustomException("您传入得参数有问题");
        }
//        初始化购物车
//        ShoppingCart shoppingCart = new ShoppingCart();
//        BeanUtils.copyProperties(shoppingCartParam,shoppingCart);
//        如果没刨异常说明id正确可以删除
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
//        减少订单 根据一下条件
        queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCartParam.getSetmealId());
        queryWrapper.eq(ShoppingCart::getDishId,shoppingCartParam.getDishId());

//        调用业务层 进行删除
        ShoppingCart shoppingCart = this.shoppingCartService.getOne(queryWrapper);
        this.shoppingCartService.removeById(shoppingCart);
        return R.success(shoppingCart);
    }
}
